// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.enums.FileBusinessTypeEnum;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.system.controller.request.MoveFileReq;
import com.liuxinlong.modules.system.controller.request.QueryFileReq;
import com.liuxinlong.modules.system.service.FileManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月30日
 */
@Controller
@RequestMapping("/file")
public class FileManegeController {
    @Autowired
    private FileManageService fileManageService;

    @Autowired
    private UploadService uploadService;

    /**
     * 查询文件管理信息列表
     *
     * @param req 请求参数
     * @return 成功标识
     */
    @PostMapping("page")
    @ResponseBody
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    BaseResp queryFileManageList(@RequestBody QueryFileReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("businessId", req.getId());
        queryParam.put("name", req.getName());
        queryParam.put("operateUser", req.getOperateUser());
        queryParam.put("source", req.getSource());
        queryParam.put("status", req.getStatus());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        List<Map<String, Object>> resultList = fileManageService.queryFileList(queryParam);
        int total = fileManageService.getFileCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(resultList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询文件信息列表成功");
        return resp;
    }

    /**
     * 查询文件管理信息
     *
     * @param req 请求参数
     * @return 成功标识
     */
    @PostMapping("query")
    @ResponseBody
    @LogAnnotation(value = "查询文件信息", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    BaseResp queryFileManageInfos(@RequestBody QueryBaseReq req) {
        Map<String, Object> resultMap = fileManageService.queryFileManageInfos(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("查询业务相关文件信息成功");
        return resp;
    }

    /**
     * 查询文件管理信息
     *
     * @param req 请求参数
     * @return 成功标识
     */
    @PostMapping("get")
    @ResponseBody
    @LogAnnotation(value = "查询文件信息", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    BaseResp getOneFileManageInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> resultMap = fileManageService.getOneFileManageInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("查询文件信息成功");
        return resp;
    }

    /**
     * 删除文件
     *
     * @param req 请求参数
     * @return 成功标识
     */
    @PostMapping("delete")
    @ResponseBody
    @LogAnnotation(value = "删除文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    BaseResp deleteFileManage(@RequestBody BaseDeleteReq req) {
        fileManageService.deleteFileManage(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除文件成功");
        return resp;
    }

    /**
     * 上传文件
     *
     * @param multipartRequest 上传信息
     * @return 文件基本信息
     */
    @PostMapping("import")
    @ResponseBody
    @LogAnnotation(value = "上传文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_IMPORT)
    BaseResp uploadFile(MultipartRequest multipartRequest) {
        int businessType = Integer.valueOf(
                ((StandardMultipartHttpServletRequest) multipartRequest).getParameterMap().get("businessType")[0]);
        Map<String, Object> map;
        if (businessType == FileBusinessTypeEnum.DESIGN_STANDARD_MASTER.getValue()) {
            map = fileManageService.uploadFileSpo(multipartRequest);
        } else  if (businessType == FileBusinessTypeEnum.BUTLER_TRAIN_MAIN.getValue()) {
            map = fileManageService.uploadFileTransfer(multipartRequest, businessType);
        } else {
            map = fileManageService.uploadFileLocal(multipartRequest, businessType);
        }

        BaseResp resp = new BaseResp();
        resp.setDataMap(map);
        resp.setRetMessage("上传文件成功");
        return resp;
    }

    /**
     * 下载文件
     *
     * @param req      请求信息
     * @param response 返回体信息
     * @return 下载文件信息
     */
    @PostMapping("download")
    @LogAnnotation(value = "下载文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    BaseResp exportFile(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        fileManageService.downloadFile(req.getId(), response);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("下载文件成功");
        return resp;
    }

    /**
     * 批量下载文件
     *
     * @param req      请求信息
     * @param response 返回体信息
     * @return 批量下载文件信息
     */
    @PostMapping("batch-download")
    @LogAnnotation(value = "批量下载文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    BaseResp batchExportFile(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        fileManageService.batchDownloadFile(req.getId(), response);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("批量下载文件成功");
        return resp;
    }

    /**
     * 预览文件
     *
     * @param req      请求信息
     * @param response 返回体信息
     * @return 预览文件信息
     */
    @PostMapping("preview")
    @LogAnnotation(value = "预览文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    void previewFile(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        fileManageService.previewFile(req.getId(), response);
//        fileManageService.previewFileSpo(req.getId(), response);
    }

    /**
     * 移动文件
     *
     * @param req 请求参数
     * @return 成功标识
     */
    @PostMapping("move")
    @ResponseBody
    @LogAnnotation(value = "移动文件", business = LogBusinessConstants.FILE_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    BaseResp moveFileManage(@RequestBody MoveFileReq req) {
        fileManageService.moveFile(req.getId(), req.getTargetHost(),req.getTargetPath());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("移动文件成功");
        return resp;
    }

    /**
     * spo文件同步
     *
     * @return 成功标识
     */
    @GetMapping("sync")
    @LogAnnotation(value = "SPO文件同步", business = LogBusinessConstants.FILE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp syncSpoFile() {
        int total = fileManageService.syncSpoFile();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("SPO文件同步成功！");
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 重试SPO文件上传
     *
     * @return 成功标识
     */
    @PostMapping("reUpload")
    @LogAnnotation(value = "重试SPO文件上传", business = LogBusinessConstants.FILE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp reUploadFile(@RequestBody BaseDeleteReq req) {
        fileManageService.reUploadFile(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("重试SPO文件上传成功！");
        return resp;
    }

    /**
     * 推送文件到三色服务器
     *
     * @return 成功标识
     */
    @PostMapping("three")
//    @LogAnnotation(value = "推送文件到三色服务器", business = LogBusinessConstants.FILE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp uploadFileThree(@RequestBody QueryBaseReq req) {
        fileManageService.uploadFileThree(req.getId());
        BaseResp resp = new BaseResp();
        resp.setTotalNum(0);
        resp.setRetMessage("推送文件到三色服务器！");
        return resp;
    }

}
